/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.ui.mon;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.Timer;

/**
 *
 * @author bennyl
 */
public class MonitorView extends javax.swing.JPanel {

    private static final Color NORMAL_COLOR = new Color(154, 204, 105);
    private static final Color SUCCESS_COLOR = new Color(102, 153, 255);
    private static final Color ERROR_COLOR = new Color(255, 51, 51);
    private Timer timer;
    private Monitor model;

    /** Creates new form MonitorView */
    public MonitorView(Monitor mon) {
        initComponents();
    }

    public MonitorView() {
        this(null);
    }

    public void setModel(final Monitor model) {
        this.model = model;
        titleLabel.setText(model.getVisualName());
//        if (model.getIcon() != null) {
//            captionLabel.setIcon(new ImageIcon(model.getIcon()));
//        }
        output.setForeground(NORMAL_COLOR);
        if (timer == null) {
            timer = new Timer(200, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Monitor mon = MonitorView.this.model;
                    if (mon != null) {
                        String append = mon.readAvailableInput();
                        double progress = mon.getProgress();
                        progressbar.setValue((int) (progress * 100));
                        output.append(append);
                        if (mon.isClosed()) {
                            timer.stop();
                        }
                    } else {
                        timer.stop();
                    }
                }
            });
        }

        if (!timer.isRunning()) {
            timer.start();
        }
    }

    public void signalError() {
        output.append("\nError Encountered!");
        output.setForeground(ERROR_COLOR);
    }

    public void signalSuccess() {
        output.append("\nOperation Completed Successfully.");
        output.setForeground(SUCCESS_COLOR);
    }

    /**
     * call this to shut down any active timers...
     */
    public void dispose() {
        if (this.timer != null) {
            this.timer.stop();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        titleLabel = new javax.swing.JLabel();
        progressbar = new javax.swing.JProgressBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        output = new javax.swing.JTextArea();

        setLayout(new java.awt.GridBagLayout());

        titleLabel.setBackground(new java.awt.Color(71, 71, 71));
        titleLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        titleLabel.setForeground(new java.awt.Color(255, 255, 255));
        titleLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/cog_go32.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(MonitorView.class, "MonitorView.titleLabel.text")); // NOI18N
        titleLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(71, 71, 71), 5));
        titleLabel.setOpaque(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(titleLabel, gridBagConstraints);

        progressbar.setBackground(new java.awt.Color(0, 0, 0));
        progressbar.setBorderPainted(false);
        progressbar.setPreferredSize(new java.awt.Dimension(146, 10));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(progressbar, gridBagConstraints);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 1, 1, 1, new java.awt.Color(81, 81, 81)));

        output.setEditable(false);
        output.setBackground(new java.awt.Color(61, 61, 61));
        output.setColumns(20);
        output.setForeground(new java.awt.Color(102, 153, 255));
        output.setLineWrap(true);
        output.setRows(5);
        output.setText(org.openide.util.NbBundle.getMessage(MonitorView.class, "MonitorView.output.text")); // NOI18N
        jScrollPane1.setViewportView(output);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        add(jScrollPane1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea output;
    private javax.swing.JProgressBar progressbar;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables
}
